package com.cardone.platform.authority.controller;

import java.util.List;
import java.util.concurrent.Callable;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.dto.ReturnDataDto;
import com.cardone.common.util.ReturnDataUtils;
import com.cardone.context.ContextHolder;
import com.cardone.platform.authority.dto.RoleUserDto;
import com.cardone.platform.authority.service.RoleUserService;

/**
 * 角色与用户
 *
 * @author yaohaitao
 */
@Deprecated
@Setter
@Slf4j
@RequestMapping("/authority/roleUser/")
@Controller
public class RoleUserController {
	/**
	 * 初始化数据绑定
	 *
	 * @param webDataBinder
	 *          数据绑定
	 */
	// @InitBinder
	// @Deprecated
	// public void initBinder(final WebDataBinder webDataBinder) {
	// }

	/**
	 * 角色与用户：删除
	 *
	 * @param deleteRoleUser
	 *          角色与用户对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "delete.json" }, method = RequestMethod.POST)
	@ResponseBody
	public Callable<ReturnDataDto<String>> deleteJson(final RoleUserDto deleteRoleUser) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RoleUserService.class).deleteByIds(deleteRoleUser);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleUserController.log);
				}
			}
		};
	}

	/**
	 * 角色与用户：分页查询
	 *
	 * @param findRoleUser
	 *          角色与用户对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "edit.html" })
	public void editHtml(final RoleUserDto findRoleUser, final Model model) {
		final RoleUserDto roleUser = ContextHolder.getBean(RoleUserService.class).findById(RoleUserDto.class, findRoleUser);

		model.addAttribute("roleUser", roleUser);
	}

	/**
	 * 角色与用户：编辑
	 *
	 * @param findRoleUser
	 *          角色与用户对象
	 * @return 实体对象集，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findById.json" })
	@ResponseBody
	public Callable<ReturnDataDto<RoleUserDto>> findByIdJson(final RoleUserDto findRoleUser, final Model model) {
		return new Callable<ReturnDataDto<RoleUserDto>>() {
			@Override
			public ReturnDataDto<RoleUserDto> call() throws Exception {
				try {
					final RoleUserDto roleUser = ContextHolder.getBean(RoleUserService.class).findById(RoleUserDto.class, findRoleUser);

					return ReturnDataUtils.newReturnData(roleUser);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleUserController.log);
				}
			}
		};
	}

	/**
	 * 角色与用户：查询
	 *
	 * @param findListRoleUser
	 *          角色与用户对象
	 * @return 角色与用户对象集合，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "findListByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<List<RoleUserDto>>> findListByLikeCodeJson(final RoleUserDto findListRoleUser) {
		return new Callable<ReturnDataDto<List<RoleUserDto>>>() {
			@Override
			public ReturnDataDto<List<RoleUserDto>> call() throws Exception {
				try {
					final List<RoleUserDto> roleUserList = ContextHolder.getBean(RoleUserService.class).findListByLikeCode(RoleUserDto.class, findListRoleUser);

					return ReturnDataUtils.newReturnData(roleUserList);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleUserController.log);
				}
			}
		};
	}

	/**
	 * 角色与用户：分页查询
	 *
	 * @param paginationRoleUser
	 *          角色与用户对象
	 * @return 分页，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "paginationByLikeCode.json" })
	@ResponseBody
	public Callable<ReturnDataDto<PaginationDto<RoleUserDto>>> paginationByLikeCodeJson(final RoleUserDto paginationRoleUser) {
		return new Callable<ReturnDataDto<PaginationDto<RoleUserDto>>>() {
			@Override
			public ReturnDataDto<PaginationDto<RoleUserDto>> call() throws Exception {
				try {
					final PaginationDto<RoleUserDto> roleUserPagination = ContextHolder.getBean(RoleUserService.class).paginationByLikeCode(RoleUserDto.class, paginationRoleUser);

					return ReturnDataUtils.newReturnData(roleUserPagination);
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleUserController.log);
				}
			}
		};
	}

	/**
	 * 角色与用户：分页查询
	 *
	 * @param paginationRoleUser
	 *          角色与用户对象
	 * @param model
	 *          上下文映射
	 */
	@Deprecated
	@RequestMapping(value = { "pagination.html" })
	public void paginationHtml(final RoleUserDto paginationRoleUser, final Model model) {
		final PaginationDto<RoleUserDto> roleUserPagination = ContextHolder.getBean(RoleUserService.class).paginationByLikeCode(RoleUserDto.class, paginationRoleUser);

		model.addAttribute("roleUserPagination", roleUserPagination);
	}

	/**
	 * 角色与用户：保存
	 *
	 * @param saveRoleUser
	 *          角色与用户对象
	 * @return 处理结果，json格式
	 */
	@Deprecated
	@RequestMapping(value = { "save.json" })
	@ResponseBody
	public Callable<ReturnDataDto<String>> saveJson(final RoleUserDto saveRoleUser) {
		return new Callable<ReturnDataDto<String>>() {
			@Override
			public ReturnDataDto<String> call() throws Exception {
				try {
					ContextHolder.getBean(RoleUserService.class).saveByIdOrCode(RoleUserDto.class, saveRoleUser);

					return ReturnDataUtils.newReturnData();
				} catch (final Exception e) {
					return ReturnDataUtils.newReturnData(e, RoleUserController.log);
				}
			}
		};
	}
}
